home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / hplip / clean < prev    next >
Encoding:
Text File  |  2007-04-04  |  5.7 KB  |  205 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  19. #
  20. # Author: Don Welch
  21. #
  22.  
  23. __version__ = '1.7'
  24. __title__ = 'Printer Cartridge Cleaning Utility'
  25. __doc__ = "Cartridge cleaning utility for HPLIP supported inkjet printers."
  26.  
  27. #Std Lib
  28. import sys
  29. import re
  30. import getopt
  31.  
  32. # Local
  33. from base.g import *
  34. from base import device, utils, maint
  35. from prnt import cups
  36.  
  37. USAGE = [(__doc__, "", "name", True),
  38.          ("Usage: hp-clean [PRINTER|DEVICE-URI] [OPTIONS]", "", "summary", True),
  39.          utils.USAGE_ARGS,
  40.          utils.USAGE_DEVICE,
  41.          utils.USAGE_PRINTER,
  42.          utils.USAGE_SPACE,
  43.          utils.USAGE_OPTIONS,
  44.          ("Cleaning level:", "-v<level> or --level=<level>", "option", False),
  45.          ("", "<level>: 1\*, 2, or 3 (\*default)", "option", False),
  46.          utils.USAGE_BUS1, utils.USAGE_BUS2,
  47.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
  48.          utils.USAGE_HELP,
  49.          utils.USAGE_EXAMPLES,
  50.          ("""Clean CUPS printer named 'hp5550':""", """$ hp-clean -php5550""",  "example", False),
  51.          ("""Clean printer with URI of 'hp:/usb/DESKJET_990C?serial=12345':""", """$ hp-clean -dhp:/usb/DESKJET_990C?serial=12345""", 'example', False),
  52.          utils.USAGE_SPACE,
  53.          utils.USAGE_NOTES,
  54.          utils.USAGE_STD_NOTES1, utils.USAGE_STD_NOTES2, 
  55.          utils.USAGE_SEEALSO,
  56.          ("hp-align", "", "seealso", False),
  57.          ("hp-colorcal", "", "seealso", False),
  58.          ]
  59.  
  60. def usage(typ='text'):
  61.     if typ == 'text':
  62.         utils.log_title(__title__, __version__)
  63.  
  64.     utils.format_text(USAGE, typ, __title__, 'hp-clean', __version__)
  65.     sys.exit(0)
  66.  
  67. log.set_module("hp-clean")
  68.  
  69. try:
  70.     opts, args = getopt.getopt(sys.argv[1:], 'p:d:hl:b:v:g',
  71.                                 ['printer=', 'device=', 'help', 'help-rest', 'help-man', 
  72.                                  'logging=', 'bus=', 'level=', 'help-desc'])
  73. except getopt.GetoptError:
  74.     usage()
  75.  
  76. bus = device.DEFAULT_PROBE_BUS
  77. log_level = logger.DEFAULT_LOG_LEVEL
  78. printer_name = None
  79. device_uri = None
  80. level = 1
  81.  
  82. if os.getenv("HPLIP_DEBUG"):
  83.     log.set_level('debug')
  84.  
  85.  
  86. for o, a in opts:
  87.     if o in ('-h', '--help'):
  88.         usage()
  89.  
  90.     elif o == '--help-rest':
  91.         usage('rest')
  92.  
  93.     elif o == '--help-man':
  94.         usage('man')
  95.  
  96.     elif o == '--help-desc':
  97.         print __doc__,
  98.         sys.exit(0)
  99.  
  100.     elif o in ('-p', '--printer'):
  101.         if a.startswith('*'):
  102.             printer_name = cups.getDefault()
  103.         else:
  104.             printer_name = a
  105.  
  106.     elif o in ('-d', '--device'):
  107.         device_uri = a
  108.  
  109.     elif o in ('-b', '--bus'):
  110.         bus = a.lower().strip()
  111.  
  112.     elif o in ('-l', '--logging'):
  113.         log_level = a.lower().strip()
  114.         if not log.set_level(log_level):
  115.             usage()
  116.  
  117.     elif o == '-g':
  118.         log.set_level('debug')
  119.  
  120.     elif o in ('-v', '--level'):
  121.         try:
  122.             level = int(a)
  123.         except ValueError:
  124.             log.error("Invalid cleaning level, setting level to 1.")
  125.             level = 1
  126.  
  127.  
  128. if level < 1 or level > 3:
  129.     log.error("Invalid cleaning level, setting level to 1.")
  130.     level = 1
  131.  
  132. if not device.validateBusList(bus):
  133.     usage()
  134.  
  135. if device_uri and printer_name:
  136.     log.error("You may not specify both a printer (-p) and a device (-d).")
  137.     usage()
  138.  
  139. utils.log_title(__title__, __version__)
  140.  
  141. if not device_uri and not printer_name:
  142.     try:
  143.         device_uri = device.getInteractiveDeviceURI(bus)
  144.         if device_uri is None:
  145.             sys.exit(0)
  146.     except Error:
  147.         log.error("Error occured during interactive mode. Exiting.")
  148.         sys.exit(0)
  149.  
  150. try:
  151.     d = device.Device(device_uri, printer_name)
  152. except Error, e:
  153.     log.error("Unable to open device: %s" % e.msg)
  154.     sys.exit(0)
  155.  
  156. if d.device_uri is None and printer_name:
  157.     log.error("Printer '%s' not found." % printer_name)
  158.     sys.exit(0)
  159.  
  160. if d.device_uri is None and device_uri:
  161.     log.error("Malformed/invalid device-uri: %s" % device_uri)
  162.     sys.exit(0)
  163.  
  164. user_cfg.last_used.device_uri = d.device_uri
  165.  
  166. try:
  167.     try:
  168.         d.open()
  169.     except Error:
  170.         log.error("Unable to print to printer. Please check device and try again.")
  171.         sys.exit(1)
  172.  
  173.     if d.isIdleAndNoError():
  174.         clean_type = d.mq.get('clean-type', 0)
  175.         log.info("Performing type %d, level %d cleaning..." % (clean_type, level))
  176.  
  177.         if clean_type in (CLEAN_TYPE_PCL,CLEAN_TYPE_PCL_WITH_PRINTOUT):
  178.             if level == 3:
  179.                 maint.wipeAndSpitType1(d)
  180.             elif level == 2:
  181.                 maint.primeType1(d)
  182.             else:
  183.                 maint.cleanType1(d)
  184.  
  185.         elif clean_type == CLEAN_TYPE_LIDIL:
  186.             if level == 3:
  187.                 maint.wipeAndSpitType2(d)
  188.             elif level == 2:
  189.                 maint.primeType2(d)
  190.             else:
  191.                 maint.cleanType2(d)
  192.  
  193.         else:
  194.             log.error("Cleaning not needed or supported on this device.")
  195.  
  196.     else:
  197.         log.error("Device is busy or in an error state. Please check device and try again.")
  198.         sys.exit(1)
  199. finally:
  200.     d.close()
  201.  
  202. log.info("")
  203. log.info("Done.")
  204.  
  205.